package com.cxy.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * 记录每次运行Service层方法的时间记录
 */
@Component
@Aspect
@Slf4j
public class TimeAspect {

    @Pointcut("execution(* com.cxy.service.*.*(..))")
    public Object recordTime(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        //1.记录开始时间
        long beginTime = System.currentTimeMillis();
        //2.调用原始方法进行
        Object result = proceedingJoinPoint.proceed();
        //3.记录结束时间，计算方法运行时间
        long endTime = System.currentTimeMillis();
        long runningTime = endTime - beginTime;
        log.info(proceedingJoinPoint.getSignature() + "方法执行耗时：{}ms",runningTime);
        return result;
    }
}
